(function(window){
    var GLOBAL = {};

    // 重写js 乘法运算
    function accMul(arg1,arg2){
        var m=0,s1=arg1.toString(),s2=arg2.toString();
        try{m+=s1.split(".")[1].length}catch(e){}
        try{m+=s2.split(".")[1].length}catch(e){}
        return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
    }

    // 是否为空
    GLOBAL.checkNull = function(option){
        if(option != null || option != "undefined" || option != ""){
            return false;
        }
        return true;
    }

    GLOBAL.share = function(){
        $('.J-share').bind("touchstart", function(event) {
            if(isWxBrowser=="qq"){
                if(window.shareSuccess){
                    shareSuccess();
                }
            }
            GLOBAL.mengban();
        });
    }

        GLOBAL.mengban = function() {
            var $mengban = $("<div class=\"mengban\"><div class=\"mengban-tip\"></div><div>");
            var $body = $("body");
            $body.append($mengban);
            $(".mengban").css({
                background: "rgba(0,0,0,0.8)",
                width: "100%",
                height: "100%",
                position: "fixed",
                left: "0",
                top: "0",
                Zindex: "1000"
            });
            $('.mengban-tip').css({
                position: "absolute"
            });
            $('.mengban').bind("touchstart", function(event) {
                $(this).remove();
            });
        }


    /**
     * 错误弹出层
     * @param  {string} msg  错误提示
     * @param  {int} time 几秒后退出，1000 等于1秒
     * @return {[type]}      [description]
     */
    GLOBAL.popMsg = function(msg,time){
        var w = $(window).width();
        var h = $(window).height();
        if($(".msg20151118")){
            $(".msg20151118").remove();
        }
        var $box = $("<div class='msg20151118'></div>");
        $box.css({
            "background" : "rgba(0,0,0,.6)",
            "width" : "80%",
            "padding" : "10px",
            "line-height" : "24px",
            "font-size" : "14px",
            "text-align" : "center",
            "border-radius" : "10px",
            "color" : "#fff"
        })
        $box.text(msg)
        $("body").append($box);
        var width = $box.width();
        var height = $box.height();
        w = (w-width)/2;
        h = (h-height)/2;
        $box.css({
            "position" : "fixed",
            "top" : h,
            "left" : w
        })
        setTimeout(function(){
            $box.remove();
        },time || 3000);
    }


    /**
     * 直接生成命名空间，以 . 分隔,不需要GLOBAL前缀
     * @param  {string} name 命名空间名字
     * @return {object}      GLOBAL全局对象
     */
    GLOBAL.namespace = function(name){
        // 拆分名字空间域字符串
        var arr = name.split(".");
            window.GLOBAL = {};
            g  = GLOBAL;

        // 循环遍历每一级子域
        for(var i = 0; i< arr.length; i++){
            var domain = arr[i];
            // 如果该域的空间未被创建
            if(typeof(g[domain]) == "undefined"){
                // 创建域
                g[domain] = {};
            }   
            // 设置当前域为此次循环的域
            g = g[domain];
        }
        return g;
    }

    // 禁止窗口上下滑动
    GLOBAL.bodyNoScroll = function(){
        var html = document.getElementsByTagName("html")[0];
        html.style.height = "100%";
        html.style.overflow = "hidden";
        document.body.style.height = "100%";
        document.body.style.overflow = "hidden";
    }

    // 开启窗口上下滑动
    GLOBAL.bodyScroll = function(){
        var html = document.getElementsByTagName("html")[0];
        html.style.height = "auto";
        html.style.overflow = "visible";
        document.body.style.height = "auto";
        document.body.style.overflow = "visible";
    }

    // 居中弹出块元素
    function winPop(option){
        var width = $(window).width();
        var height = $(window).height();
        $(option).show();
        var w = $(option).width();
        var h = $(option).height();
        w = parseInt(width-w)/2;
        h = parseInt(height-h)/2;
        $(option).css({
            "position" : "fixed",
            "top" : h,
            "left" : w,
            "z-index" : "100001"
        })
    }

    /**
     * 自定义confirm弹出框
     * @param  {string}   option   弹出框的id或class
     * @param  {Function} callback 确定后的回调函数
     * @return {boolean}            false
     */
    function confirmA(option,callback){
        var w = $(window).width();
        var h = $(window).height();

        var ele = $("<div></div>");
        $("body").append(ele);
        ele.css({
            "position": 'fixed',
            "left": 0,
            "top" : 0,
            "width" : "100%",
            "height" : "100%",
            "background" : "rgba(0,0,0,0.3)"
        });
        $(option).show();
        var optionW = $(option).width();
        var optionH = $(option).height();
        w = (w-optionW)/2;
        h = (h-optionH)/2;
        $(option).css({
            "position" : "fixed",
            "left" : w,
            "top" : h
        });
        $(option).find("#J_ok").unbind("click").click(function(){
            $(option).hide();
            ele.remove();
            if(typeof(callback) == "function"){
                callback();
                return false;
            }
        });
        $(option).find("#J_no").unbind("click").click(function(){
            $(option).hide();
            ele.remove();
            return false;
        });
        $(option).find("#J_winClose").unbind("click").click(function(){
            $(option).hide();
            ele.remove();
            return false;
        })

    }

    /**
     * 错误弹出层
     * @param  {string} msg  错误提示
     * @param  {int} time 几秒后退出，1000 等于1秒
     * @return {[type]}      [description]
     */
    GLOBAL.pop = function(msg,time,callback){
        var w = $(window).width();
        var h = $(window).height();
        if($(".msg20151118")){
            $(".msg20151118").remove();
        }
        var $box = $("<div class='msg20151118'></div>");
        $box.css({
            "background" : "rgba(0,0,0,.6)",
            "box-sizing" : "border-box",
            "width" : "80%",
            "padding" : "10px",
            "line-height" : "24px",
            "font-size" : "14px",
            "text-align" : "center",
            "border-radius" : "10px",
            "color" : "#fff",
            "z-index": "1000"
        })
        $box.text(msg)
        $("body").append($box);
        var width = $box.width();
        var height = $box.height();
        // w = (w-width)/2;
        // h = (h-height)/2;
        $box.css({
            "position" : "fixed",
            "top" : "40%",
            "left" : "10%"
        })
        setTimeout(function(){
            $box.remove();
            if(typeof(callback) == "function"){
                callback();
                return;
            }
        },time || 3000);
    }

    /**
     * 给当前元素添加class，移除兄弟元素的class
     * @param  {string} option 当前元素挂钩
     * @param  {string} hook   所要添加的类
     * @return {[type]}        [description]
     */
    GLOBAL.tabChange = function(option,hook){
        if(option != "" && hook != ""){
            $(option).click(function(){
                $(this).addClass(hook).siblings().removeClass(hook);
            })
        }
    }

    GLOBAL.showLoading = function(){
        $('.J_loading').show();
    }

    GLOBAL.hideLoading = function(){
        $('.J_loading').hide();
    }

    GLOBAL.backTop = function() {
        var backtop = $("<div><em></em></div>");
        var h = $(window).height();
        var w = $(window).width();
        w = (w - 1000) / 2 - 20 - 50;
        backtop.css({
            "width": '40px',
            "height": '40px',
            "background-size": "100% 100%",
            "background-image": "url()",
            "position": "fixed",
            "right": 0,
            "bottom": ".6rem",
            "cursor": "pointer",
            "display": "none"
        });
       
        $("body").append(backtop);

        backtop.click(function () {
            var timer = setInterval(function () {
                $(window).scrollTop($(window).scrollTop() - 100);
                if ($(window).scrollTop() <= 0) {
                    clearInterval(timer);
                }
            }, 10);
        })

        $(window).scroll(function () {
            if ($(window).scrollTop() < h) {
                backtop.hide();
            } else {
                backtop.show();
            }
        })
    }

    window.GLOBAL = GLOBAL;
})(window);
